/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jmicroerp.wicket.components.grid;

import com.googlecode.jmicroerp.model.AbstractEntitate;
import com.googlecode.jmicroerp.model.Active;
import com.googlecode.jmicroerp.repository.GenericOrder;
import com.googlecode.jmicroerp.repository.GenericProperty;
import com.googlecode.jmicroerp.repository.GenericRestriction;
import com.googlecode.jmicroerp.service.GenericService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/**
 *
 * @author Alexandru.Codescu
 */
@Service(value = "defaultActiveGridAction")
@Scope(value = "prototype")
public class DefaultActiveGridAction <T extends Active> extends DefaultGridAction<T> {

    private static final long serialVersionUID = 1L;
    @Resource(name="genericService")
    private GenericService genericService;

    private GenericOrder getGenericOrder(SortParam sort) {
        if (sort == null) {
            return null;
        }
        String property = sort.getProperty().toString();
        GenericProperty genericProperty = GenericProperty.getInstance(property);

        return new GenericOrder(genericProperty, sort.isAscending());
    }
    
    private List<GenericRestriction> getRealRestrictions(List<GenericRestriction> genericProperties) {
        List<GenericRestriction> realRestrictions = new ArrayList<GenericRestriction>();
        if (genericProperties != null) {
            realRestrictions.addAll(genericProperties);
        }
        realRestrictions.add(new GenericRestriction(new GenericProperty("activ"), Boolean.TRUE));
        return realRestrictions;
    }
    
    @Override
    public Iterator<T> getList(Class<T> persistentClass, List<GenericRestriction> genericProperties, List<GenericOrder> genericOrdersParameter, SortParam sort, long firstResult, long maxResults) {
    	List<GenericRestriction> realRestrictions = getRealRestrictions(genericProperties);
    	return super.getList(persistentClass, realRestrictions, genericOrdersParameter, sort, firstResult, maxResults);
    }

    @Override
    public long getCount(Class<T> persistentClass, List<GenericRestriction> genericProperties) {
        List<GenericRestriction> realRestrictions = getRealRestrictions(genericProperties);
        return super.getCount(persistentClass, realRestrictions);
    }
}
